package com.example.mymk2_17;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    public static DataInputStream dataInputStream = null; //输入流
    public static DataOutputStream dataOutputStream =null;//输出流

    private byte[] buff = new byte[10];  //接收缓冲区
    private byte[] sendbuff = new byte[5];  //发送接收区
    TextView ster;
    private Button btn_conn;
    private Button btn_close;
    private Button btn_jia;
    private Button btn_jian;
    private EditText et_ip;
    private EditText et_port;
    private  String ip;//服务器IP地址
    private int port;//服务器端口
    public static Socket socket = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ster=this.findViewById(R.id.ster);
        et_ip=findViewById(R.id.et_ip);
        et_port=findViewById(R.id.et_port);
        btn_conn = (Button) findViewById(R.id.btn_conn);
        btn_close = (Button) findViewById(R.id.btn_colse);
        btn_jia = (Button) findViewById(R.id.btn_jia);
        btn_jian = (Button) findViewById(R.id.btn_jian);
        btn_conn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ster.setText("已连接服务器！");
                startThread();
            }
        });
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(socket!=null && socket.isConnected()){
                    try{
                        socket.close();
                        ster.setText("已断开服务器！");
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        });
        btn_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            formData((byte)1);
                            dataOutputStream.write(sendbuff);
                        }catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }catch(IOException e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        btn_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            formData((byte)0);
                            dataOutputStream.write(sendbuff);
                        }catch (UnsupportedEncodingException e){
                            e.printStackTrace();
                        }catch(IOException e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
    private void startThread()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ip=et_ip.getText().toString().trim();
                port=Integer.parseInt(et_port.getText().toString().trim());
                try {
                    socket = new Socket(ip,port);
                    dataInputStream = new DataInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void formData(byte oper)
    {
        sendbuff[0] = (byte)0x55;
        sendbuff[1] = (byte)0xBB;
        sendbuff[2] = (byte)0x00;
        sendbuff[3] = (byte)0x08;
        sendbuff[4] = (byte)oper;
    }
}